package com.sky.controller.user;


import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.result.Result;
import com.sky.service.ShoppingCartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "购物车相关接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;


    /**
     * 购物车添加数据
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("购物车已创建，菜品信息：",shoppingCartDTO);

        shoppingCartService.add(shoppingCartDTO);

        return Result.success();
    }

    /**
     * 前端页面展示查询购物车数据
     */
    @GetMapping ("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> display(){

        List<ShoppingCart> list = shoppingCartService.display();

        return Result.success(list);
    }

    /**
     * 减商品或者删除商品
     */
    @PostMapping("/sub")
    @ApiOperation("减商品或者删除商品")
    public Result sub(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("减少一个菜品或套餐：{}",shoppingCartDTO);

        shoppingCartService.sub(shoppingCartDTO);

        return Result.success();
    }

    /**
     * 清空购物车
     */
    @DeleteMapping("clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        shoppingCartService.clean();

        return Result.success();
    }
}
